package com.yylending.las.authority.gestures;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CompoundButton;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yylending.las.R;
import com.yylending.las.YJKD;
import com.yylending.las.authority.login.LoginActivity;
import com.yylending.las.authority.password.ForgetPwdActivity;
import com.yylending.las.authority.password.ValidatePasswordPresenter;

/**
 * Created by HenryZhang on 2016/11/21.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class ResetGesturesActivity extends ModifyGesturesActivity implements ValidatePasswordPresenter.View,
		View.OnClickListener, CompoundButton.OnCheckedChangeListener, TextWatcher {

	private final static String TAG = "ResetGesturesActivity";

	private final static int LOADER_ID = 1;
	private final static String EXTRA_BUSINESS = "extra_business";

	private ValidatePasswordPresenter mPresenter;

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		switch (loader.getId()) {
			case LOADER_ID:
				mPresenter = (ValidatePasswordPresenter) data;
				mPresenter.attach(this);
				break;
		}
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		switch (id) {
			case LOADER_ID:
				return new PresenterLoader<>(this, new ValidatePasswordPresenter.Generator());
		}
		return null;
	}

	public static void start(Activity aty) {
		Intent i = new Intent(aty, ResetGesturesActivity.class);
		i.putExtra(EXTRA_BUSINESS, ModifyGesturesActivity.GESTURE_RESET);
		aty.startActivity(i);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mResetPassword.addTextChangedListener(this);
		mResetDelete.setOnClickListener(this);
		mEyesOpen.setOnCheckedChangeListener(this);
		mForgot.setOnClickListener(this);
		mNext.setOnClickListener(this);
		initView();

		getSupportLoaderManager().initLoader(LOADER_ID, null, this);
	}

	private void initView() {
		mGestureModifyContainer.setVisibility(View.GONE);
		mGesturesResetContainer.setVisibility(View.VISIBLE);
	}

	@Override
	public void onClick(View view) {
		super.onClick(view);
		switch (view.getId()) {
			case R.id.gestures_reset_delete:
				mResetPassword.setText("");
				break;
			case R.id.gestures_reset_forgot:
				YJKD.hideInputWindow(this);
				ForgetPwdActivity.start(this, "");
				break;
			case R.id.gestures_reset_next:
				YJKD.hideInputWindow(this);
				if (YJKD.login()) {
					String password = mResetPassword.getText().toString().trim();
					if (TextUtils.isEmpty(password)) {
						showToast(R.string.input_password);
					} else {
						mPresenter.validatePwd(password);
					}
				} else {
					LoginActivity.start(this);
				}
				break;
		}
	}

	@Override
	protected void onDestroy() {
		getSupportLoaderManager().destroyLoader(LOADER_ID);
		super.onDestroy();
	}

	@Override
	public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
		if(checked){
			mResetPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
		}else{
			mResetPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
		}
		mResetPassword.setSelection(mResetPassword.getText().length());
	}

	@Override
	public void validatePwdSuccess() {
		YJKD.clearGesture(false);
		mGesturesResetContainer.setVisibility(View.GONE);
		mGestureModifyContainer.setVisibility(View.VISIBLE);
	}

	@Override
	public void validatePwdFail(String msg) {
		showToast(R.string.pwd_error);
	}

	@Override
	public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
	}

	@Override
	public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
		if (charSequence.length() > 0)
			mResetDelete.setVisibility(View.VISIBLE);
		else
			mResetDelete.setVisibility(View.GONE);
	}

	@Override
	public void afterTextChanged(Editable editable) {
	}
}
